package com.i8office.android.zhihuixinwu.ui.schedule;


import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.TimePicker;

import com.afollestad.materialdialogs.MaterialDialog;
import com.i8office.android.zhihuixinwu.R;
import com.socks.library.KLog;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import view.recyclerview.CommonAdapter;
import view.recyclerview.ViewHolder;

/**
 * Created by jiajia on 2018/3/23.
 */
public class ChooseTimeDialogFragment extends DialogFragment {
    @BindView(R.id.time)
    TextView mTime;
    @BindView(R.id.remind)
    TextView mRemind;
    private List<String> mData;
    private OnChooseTime onChooseTime;
    private String[] strs;
    private MaterialDialog remindDialog;
    private String checkStr;


    public static ChooseTimeDialogFragment newInstance(String time, String remind) {

        Bundle args = new Bundle();
        args.putString("time", time);
        args.putString("remind", remind);
        ChooseTimeDialogFragment fragment = new ChooseTimeDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.dialog_fragment_choose_time, null);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        strs = getResources().getStringArray(R.array.reminds);
        mTime.setText(getArguments().getString("time"));

        mRemind.setText(getArguments().getString("remind"));

//        final List<String> list = new ArrayList<>();
//        for (String str : strs) {
//            list.add(str);
//        }
//        mRemind.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list));
//        int position = list.indexOf(getArguments().getString("remind"));
//        mRemind.setSelection(position == -1 ? 0 : position);
//        mRemind.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//                if (onChooseTime != null) {
//                    onChooseTime.onChooseRemind(list.get(i));
//                }
//            }
//
//            @Override
//            public void onNothingSelected(AdapterView<?> adapterView) {
//
//            }
//        });

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnChooseTime) {
            onChooseTime = (OnChooseTime) context;
        }
    }

    @OnClick(R.id.no_time)
    void noTime() {
        if (onChooseTime != null) {
            onChooseTime.onChooseTime("", 0, 0, 0);
            onChooseTime.onChooseRemind("", 0, 0, 0);
            dismissAllowingStateLoss();
        }
    }

    @OnClick(R.id.remind_layout)
    void chooseRemind() {
        checkStr = mRemind.getText().toString();
        if (checkStr.equals("")) {
            checkStr = strs[0];
        }
        List<String> items = new ArrayList<>(Arrays.asList(strs));
        remindDialog = new MaterialDialog.Builder(getActivity())
                .adapter(new CommonAdapter<String>(getActivity(), R.layout.item_remind, items) {
                    @Override
                    protected void convert(ViewHolder holder, final String s, final int position) {
                        holder.setText(R.id.text, s);
                        holder.getView(R.id.img).setSelected(s.equals(checkStr));
                        holder.getConvertView().setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {

                                String remind = s;
                                int d = 0;
                                int h = 0;
                                int m = 0;
                                switch (position) {
                                    case 0:
                                        remind = "";
                                        break;
                                    case 2:
                                        m = 10;
                                        break;
                                    case 3:
                                        m = 20;
                                        break;
                                    case 4:
                                        m = 30;
                                        break;
                                    case 5:
                                        h = 1;
                                        break;
                                    case 6:
                                        h = 2;
                                        break;
                                    case 7:
                                        h = 4;
                                        break;
                                    case 8:
                                        h = 8;
                                        break;
                                    case 9:
                                        h = 12;
                                        break;
                                    case 10:
                                        d = 1;
                                        break;
                                    case 11:
                                        d = 2;
                                        break;
                                }
                                mRemind.setText(remind);
                                if (onChooseTime != null) {
                                    onChooseTime.onChooseRemind(remind, d, h, m);
                                }
                                remindDialog.dismiss();
                            }
                        });
                    }
                }, new LinearLayoutManager(getActivity()))
                .show();
    }

    @OnClick(R.id.time_layout)
    void chooseTime() {
        final Calendar calendar = Calendar.getInstance();
        new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int h, int m) {
                KLog.e(h + ":" + m);
                SimpleDateFormat format = new SimpleDateFormat("HH:mm");
                calendar.set(Calendar.HOUR_OF_DAY, h);
                calendar.set(Calendar.MINUTE, m);
                int apm = calendar.get(Calendar.AM_PM) + 1;
//                String apmStr;
//                if (h < 11) {
//                    apmStr = "上午";
//                } else if (h < 13) {
//                    apmStr = "中午";
//                } else {
//                    apmStr = "下午";
//                }
//                String apmStr = apm == 1 ? "上午" : "下午";
                String time = format.format(calendar.getTimeInMillis());
//                int h1 = h > 12 ? (h - 12) : h;
                mTime.setText(time);
                if (onChooseTime != null) {
                    onChooseTime.onChooseTime(time, h, m, apm);
                }
            }
        }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true).show();

    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // 使用不带theme的构造器，获得的dialog边框距离屏幕仍有几毫米的缝隙。
        // Dialog dialog = new Dialog(getActivity());
        Dialog dialog = new Dialog(getActivity(), R.style.CustomDatePickerDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // must be called before set content
        dialog.getWindow().setBackgroundDrawableResource(R.drawable.login_dialog_bg);
        dialog.getWindow().getAttributes().windowAnimations = R.style.dialogAnim;
        dialog.getWindow().setContentView(R.layout.dialog_fragment_choose_time);
        dialog.setCanceledOnTouchOutside(true);
        // 设置宽度为屏宽、靠近屏幕底部。
        Window window = dialog.getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.BOTTOM;
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(wlp);
        return dialog;
    }


    public interface OnChooseTime {
        void onChooseTime(String time, int h, int m, int apm);

        void onChooseRemind(String remind, int d, int h, int m);
    }
}
